﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Excel = Microsoft.Office.Interop.Excel;
using ExcelAddIn2010.ExcelOp;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using Scrt;
using DalMgr;
using System.Diagnostics;
using System.Threading;
namespace ExcelAddIn2010
{
    public partial class MainRibbon
    {
        SoftMgr softMgr;
        Thread ThBack = null;
        private void MainRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            this.grupAboutUs.Label = gValue.STUDIO_NAME;
            softMgr = new SoftMgr();
            EnabledRibbonControls(gValue.softReg.RegResult == Scrt.RegisterResult.SUCCEED);
            ThBack = new Thread(BackgroundProc);
            ThBack.Start();
        }
        private void BackgroundProc()
        {
            if (Funcs.IsANewDay())
            {
                CheckUpdate();
            }
            SetDisHasNewVersion();
        }
        private void MainRibbon_Close(object sender, EventArgs e)
        {
            softMgr = null;
            if (ThBack != null)
            {
                ThBack.Abort();
            }
        }
        private void EnabledRibbonControls(bool newState)
        {
            for (int idx = 0; idx < this.TabMain.Groups.Count; idx++)
            {
                RibbonGroup grup = this.TabMain.Groups[idx];
                if (grup.Name == this.grupAboutUs.Name)
                {
                    continue;
                }
                for (int i = 0; i < grup.Items.Count; i++)
                {
                    grup.Items[i].Enabled = newState;
                }
                grup = null;
            }
        }
        private void btnAboutAuthor_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.ShowAuthor();
        }

        private void btnRegister_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.ShowRegForm();
            EnabledRibbonControls(gValue.softReg.RegResult == Scrt.RegisterResult.SUCCEED);
        }
        private void btnCheckUpdate_Click(object sender, RibbonControlEventArgs e)
        {
            if (!Funcs.CheckNetConnection())
            {
                MessageBox.Show("检查网络连接","提醒",MessageBoxButtons.OK);
                return;
            }
            SoftwareUpdateInfor updateInfo = CheckUpdate();
            if (updateInfo.NeedUpdate)
            {
                string str = string.Format("当前版本:V{0}\r\n升级版本:V{1}\r\n\r\n是否打开升级网址?", updateInfo.CurrentVersion, updateInfo.UpdateVersion);
                if (MessageBox.Show(str,"提醒", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Process.Start(updateInfo.UpdateUrl);
                    SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_HAS_NEW_VERSION, false);
                }
            }
            else
            {
                MessageBox.Show("该软件已经为最新", "提醒");
            }
            SetDisHasNewVersion();
        }

        private void btnPurchase_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.OpenUrl(gValue.URL_ITEM_PURCHASE);
        }
        private void btnDeleteBlankRow_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.DeleteBlankRows();
        }
        private void btnDeleteBlankWorksheet_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.DeleteBlankSheets();
        }
        private void btnDeleteWorksheet_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.DeleteSheets();
        }

        private void galleryWksht_ItemsLoading(object sender, RibbonControlEventArgs e)
        {
            RibbonMenu ribbonMenu = (RibbonMenu)sender;
            ribbonMenu.Items.Clear();
            string[] ArrNames = WorkbookOp.GetSheetsName();
            foreach (string str in ArrNames)
            {
                RibbonButton btn = this.Factory.CreateRibbonButton();
                btn.Name = str;
                btn.Label = str;
                btn.Click += new RibbonControlEventHandler(wkShtActive);
                ribbonMenu.Items.Add(btn);
                btn = null;
            }
        }
        private void wkShtActive(object sender, RibbonControlEventArgs e)
        {
            RibbonButton btn = (RibbonButton)sender;
            Excel.Workbook wkBk = gValue.ExcelApp.ActiveWorkbook;
            if (WorkbookOp.HasSheet(wkBk, btn.Label))
            {
                wkBk.Worksheets[btn.Label].Select();
            }
            wkBk = null;
        }

        private void btnHomeUrl_Click(object sender, RibbonControlEventArgs e)
        {
            softMgr.OpenHomeUrl();
        }
        private void btnContent_Click(object sender, RibbonControlEventArgs e)
        {
            WorkbookOp.CreatWorkShtContent(WKSHT_CONTENT.NORMAL);
        }
        private void btnContentHide_Click(object sender, RibbonControlEventArgs e)
        {
            WorkbookOp.CreatWorkShtContent(WKSHT_CONTENT.NORMAL_HIDE);
        }
        private void btnContentBack_Click(object sender, RibbonControlEventArgs e)
        {
            WorkbookOp.CreatWorkShtContent(WKSHT_CONTENT.BACK);
        }
        private void btnContentBackHide_Click(object sender, RibbonControlEventArgs e)
        {
            WorkbookOp.CreatWorkShtContent(WKSHT_CONTENT.BACK_HIDE);
        }
        private void btnDeleteComment_Click(object sender, RibbonControlEventArgs e)
        {
            WorksheetOp.DeleteAllComments();
        }
        private void btnDeleteHyperLink_Click(object sender, RibbonControlEventArgs e)
        {
            WorksheetOp.DeleteAllHyperlinks();
        }
        private void btnRemoveWorksheetProtectPassword_Click(object sender, RibbonControlEventArgs e)
        {
            Worksheet wkSht = gValue.ExcelApp.ActiveSheet;
            
            if (wkSht.ProtectContents)
            {
                WorksheetOp.RemoveWkshtPassword(wkSht);
                if (wkSht.ProtectContents)
                {
                    MessageBox.Show("破解密码失败", "密码移除", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show("破解密码成功", "密码移除", MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show("此工作表无密码！", "密码移除", MessageBoxButtons.OK);
            }
        }
        private void btnRemoveAllWorksheetProtectPassword_Click(object sender, RibbonControlEventArgs e)
        {
            Excel.Workbook wkBk = gValue.ExcelApp.ActiveWorkbook;
            foreach (Excel.Worksheet wkSht in wkBk.Worksheets)
            {
                WorksheetOp.RemoveWkshtPassword(wkSht);
            }
        }
        private void btnRemoveWorkbookProtectPassword_Click(object sender, RibbonControlEventArgs e)
        {
            Excel.Workbook wkBk = gValue.ExcelApp.ActiveWorkbook;
            if (wkBk.ProtectStructure || wkBk.ProtectWindows)
            {
                WorkbookOp.RemoveWkbkPassword(wkBk);
                if (wkBk.ProtectStructure || wkBk.ProtectWindows)
                {
                    MessageBox.Show("破解密码失败", "密码移除", MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show("此工作簿无密码！", "密码移除", MessageBoxButtons.OK);
            }
        }
        private void btnRemoveVBAPsd_Click(object sender, RibbonControlEventArgs e)
        {
            string filePath = Funcs.GetFilePath("Excel文档(*.xls)|*.xls");
            if (filePath == "")
            {
                return;
            }
            filePath = WorkbookOp.RemoveVBAPsd(filePath);
            if (filePath != "")
            {
                MessageBox.Show(filePath);
            }
            else
            {
                MessageBox.Show("移除VBA工程密码成功", "密码移除", MessageBoxButtons.OK);
            }
        }
        private SoftwareUpdateInfor CheckUpdate()
        {
            SoftwareUpdateInfor updateInfo = new SoftwareUpdateInfor(gValue.SOFT_INFO_URL,Funcs.GetSftVersion());
            SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_HAS_NEW_VERSION, updateInfo.NeedUpdate);
            gValue.SftwareDownloadUrl = updateInfo.UpdateUrl;
            SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_DOWNLOAD_URL, gValue.SftwareDownloadUrl);
            return updateInfo;
        }

        private void btnHasNewVersion_Click(object sender, RibbonControlEventArgs e)
        {
            if (gValue.SftwareDownloadUrl != "")
            {
                softMgr.OpenUrl(gValue.SftwareDownloadUrl);
            }
        }
        private void SetDisHasNewVersion(bool bDis)
        {
            SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_HAS_NEW_VERSION, bDis);
            SetDisHasNewVersion();
        }
        private void SetDisHasNewVersion()
        {
            bool bDis = SftDataMgr.ReadBool(gValue.SFT_CFG, gValue.SFT_CFG_HAS_NEW_VERSION, false);
            this.btnHasNewVersion.Visible = bDis;
            if (bDis)
            {
                this.grupAboutUs.Label = "新版本出现了";
            }
            else
            {
                this.grupAboutUs.Label = gValue.STUDIO_NAME;
            }
        }
    }
}
